const router = require("koa-router")();
const Mock = require("mockjs");
const JWT = require("jsonwebtoken");
const OSS = require("ali-oss");

const client = new OSS({
  // yourregion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
  region: "oss-cn-shanghai",
  // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  accessKeyId: `LTAI5tGq86RaeKPBgxX9XXWo`,
  accessKeySecret: `OqDYDhiCTc3gpS1lWX7aMHBFURiSts`,
  // 填写Bucket名称。
  bucket: "2024-4",
});

router.post("/upload/oss", async (ctx) => {
  const { originalFilename, filepath } = ctx.request.files.file;
  const result = await client.put(originalFilename, filepath);
  ctx.body = {
    code: 200,
    msg: "上传成功",
    data: result,
  };
});

router.get("/upload/list", async (ctx) => {
  const resp = await client.list();
  ctx.body = {
    code: 200,
    data: resp,
  };
});

let user = [
  {
    username: "admin",
    password: "admin",
    row: "admin",
    info: "https://t7.baidu.com/it/u=3713375227,571533122&fm=193&f=GIF",
  },
  {
    username: "user",
    password: "user",
    row: "user",
    info: "https://t7.baidu.com/it/u=1723458001,3671360301&fm=193&f=GIF",
  },
];
router.post("/sigin", (ctx) => {
  let { username } = ctx.request.body;
  let items = user.find((item) => item.username == username);
  if (items) {
    ctx.body = {
      code: 400,
      msg: "账号已存在",
    };
  } else {
    user.push({
      ...ctx.request.body,
      row: "admin",
      info: "https://t7.baidu.com/it/u=2731426114,1290998454&fm=193&f=GIF",
    });
    ctx.body = {
      code: 200,
      msg: "注册成功",
    };
  }
});
router.post("/login", (ctx) => {
  let { username, password } = ctx.request.body;
  let items = user.find(
    (item) => item.username == username && item.password == password
  );
  if (items) {
    ctx.body = {
      code: 200,
      msg: "登录成功",
      token: JWT.sign(items.username, "1231231"),
      row: items.row,
      user: items.username,
      info: items.info,
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "登录失败",
    };
  }
});

let { list } = Mock.mock({
  "list|100": [
    {
      id: "@id",
      title: "@ctitle(2)",
      county: "@county(true)",
      word: "@cword(5)",
      gj: "中国",
    },
  ],
});

router.get("/list", (ctx) => {
  ctx.body = {
    code: 200,
    list,
  };
});

router.put("/edit", (ctx) => {
  let { id } = ctx.request.body;
  let items = list.find((item) => item.id == id);
  if (items) {
    Object.assign(items, ctx.request.body);
    ctx.body = {
      code: 200,
      msg: "修改成功",
    };
  } else {
    ctx.body = {
      code: 400,
      msg: "修改失败",
    };
  }
});

router.put("/add", (ctx) => {
  list.unshift({ ...ctx.request.body, id: Mock.mock("@id") });
  ctx.body = {
    code: 200,
    msg: "添加成功",
  };
});

router.delete("/del", (ctx) => {
  let { id } = ctx.request.query;
  list = list.filter((item) => item.id !== id);
  ctx.body = {
    code: 200,
    msg: "删除成功",
  };
});

router.get("/search", (ctx) => {
  let { value } = ctx.request.query;
  if (value !== "") {
    let items = list.filter(
      (item) =>
        item.id.includes(value) ||
        item.title.includes(value) ||
        item.county.includes(value) ||
        item.word.includes(value) ||
        item.gj.includes(value)
    );
    ctx.body = {
      code: 200,
      items: items,
    };
  } else {
    ctx.body = {
      items: list,
    };
  }
});


let listk=[];
let books=['你好','您有什么问题',"再见",'今天天气不错','哈哈哈']

router.post('/lt',(ctx,next)=>{
  let index=Math.floor(Math.random() * 4) + 0 //0-4
  let {username}=ctx.request.body
  if (username!=='') {
    ctx.body={
      code:200,
      msg:books[index]
    }
  }else{
    ctx.body={
      code:400,
      msg:"不能为空"
    }
  }
  
})
module.exports = router;
